package com.wayne.pojo;

import com.wayne.binding.MapperRegistry;
import com.wayne.sqlSession.SqlSession;

import javax.sql.DataSource;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

public class Configuration {
  private DataSource dataSource;
  private MapperRegistry mapperRegistry;
  // key statementId value: 封装好的MappedStatement对象
  private Map<String, MappedStatement> mappedStatementMap = new ConcurrentHashMap<>();
  protected final Set<String> loadedResources = new HashSet<>();

  public Configuration() {
    mapperRegistry = new MapperRegistry(this);
  }

  public DataSource getDataSource() {
    return dataSource;
  }

  public void setDataSource(DataSource dataSource) {
    this.dataSource = dataSource;
  }

  public Map<String, MappedStatement> getMappedStatementMap() {
    return mappedStatementMap;
  }

  public void setMappedStatementMap(Map<String, MappedStatement> mappedStatementMap) {
    this.mappedStatementMap = mappedStatementMap;
  }

  public MappedStatement getMappedStatement(String statementId) {
    return mappedStatementMap.get(statementId);
  }

  public boolean hasStatement(String statementId) {
    return mappedStatementMap.containsKey(statementId);
  }

  public void addLoadedResource(String resource) {
    loadedResources.add(resource);
  }

  public boolean isResourceLoaded(String resource) {
    return loadedResources.contains(resource);
  }

  public <T> T getMapper(Class<T> type, SqlSession sqlSession) {
    return mapperRegistry.getMapper(type, sqlSession);
  }

  public MapperRegistry getMapperRegistry() {
    return mapperRegistry;
  }
}
